package cn.iocoder.yudao.module.wms.controller.admin.rdrecord32;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.rdrecord32.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord32.Rdrecord32DO;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecords32.Rdrecords32DO;
import cn.iocoder.yudao.module.wms.service.rdrecord32.Rdrecord32Service;

@Tag(name = "管理后台 - 销售出库主")
@RestController
@RequestMapping("/wms/rdrecord32")
@Validated
public class Rdrecord32Controller {

    @Resource
    private Rdrecord32Service rdrecord32Service;

    @PostMapping("/create")
    @Operation(summary = "创建销售出库主")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord32:create')")
    public CommonResult<Integer> createRdrecord32(@Valid @RequestBody Rdrecord32SaveReqVO createReqVO) {
        return success(rdrecord32Service.createRdrecord32(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新销售出库主")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord32:update')")
    public CommonResult<Boolean> updateRdrecord32(@Valid @RequestBody Rdrecord32SaveReqVO updateReqVO) {
        rdrecord32Service.updateRdrecord32(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除销售出库主")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:rdrecord32:delete')")
    public CommonResult<Boolean> deleteRdrecord32(@RequestParam("id") Integer id) {
        rdrecord32Service.deleteRdrecord32(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得销售出库主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord32:query')")
    public CommonResult<Rdrecord32RespVO> getRdrecord32(@RequestParam("id") String id) {
        try {
            Rdrecord32DO rdrecord32 = rdrecord32Service.getRdrecord32(Integer.valueOf(id));
            return success(BeanUtils.toBean(rdrecord32, Rdrecord32RespVO.class));
        }catch (Exception e)
        {
            Rdrecord32DO rdrecord32 = rdrecord32Service.findByProcessInstanceId(id);
            return success(BeanUtils.toBean(rdrecord32, Rdrecord32RespVO.class));

        }
    }

    @GetMapping("/page")
    @Operation(summary = "获得销售出库主分页")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord32:query')")
    public CommonResult<PageResult<Rdrecord32RespVO>> getRdrecord32Page(@Valid Rdrecord32PageReqVO pageReqVO) {
        PageResult<Rdrecord32DO> pageResult = rdrecord32Service.getRdrecord32Page(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Rdrecord32RespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出销售出库主 Excel")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord32:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRdrecord32Excel(@Valid Rdrecord32PageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Rdrecord32DO> list = rdrecord32Service.getRdrecord32Page(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "销售出库主.xls", "数据", Rdrecord32RespVO.class,
                        BeanUtils.toBean(list, Rdrecord32RespVO.class));
    }

    @GetMapping("/startProcess")
    @Operation(summary = "启动流程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:rdrecord32:create')")
    public CommonResult<Boolean> startProcess(@RequestParam("id") Integer id) {
        rdrecord32Service.process(id);
        return success(true);
    }

    // ==================== 子表（销售出库子） ====================

    @GetMapping("/rdrecords32/list-by-pid")
    @Operation(summary = "获得销售出库子列表")
    @Parameter(name = "pid", description = "订单id")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord32:query')")
    public CommonResult<List<Rdrecords32DO>> getRdrecords32ListByPid(@RequestParam("pid") Integer pid) {
        return success(rdrecord32Service.getRdrecords32ListByPid(pid));
    }

}